package com.admin.servlet;

import com.admin.service.IAdminService;
import com.admin.service.impl.AdminServiceImpl;
import com.user.entity.BookInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "AddBookServlet", value = "/AddBookServlet")
public class AddBookServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String adminname = (String) request.getSession().getAttribute("adminname");
        String bookname = request.getParameter("bookname");
        String category = request.getParameter("category");
        String author = request.getParameter("author");
        int number = Integer.parseInt(request.getParameter("number"));
        BookInfo bookInfo = new BookInfo(0, bookname, author, category, number);
        IAdminService adminService = new AdminServiceImpl();
        if (adminService.addBooks(bookInfo)) {
            request.setAttribute("msg", "添加成功！");

        } else
            request.setAttribute("msg", "添加失败！");
        request.getRequestDispatcher("/BookListServlet?adminname=" + adminname).forward(request, response);
        return;


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
